class BackupVolume

  attr_accessor :lines

  def initialize
    @details_script = "select disk=1 \ndetail disk"
    @details_script_file = "c:/getdetails.txt"
    @details_output_file = "c:/details.txt"
    @unfreeze_script_file = "c:/unfreeze.txt"
  end

  def get_disk_details
    File.open(@details_script_file, 'w') { |f| f.puts @details_script }
    `diskpart /s #{@details_script_file} > #{@details_output_file}`
    @lines = File.open(@details_output_file).readlines
    save @unfreeze_script_file
  end

  def freeze()
    `mountvol.exe D: /p`
  end

  def unfreeze()
    `diskpart /s #{@unfreeze_script_file}`
  end

  def volume_info
    @lines.select {|line| line.include? "Volume"}.last
  end

  def volume_number
    volume_info.split[1]
  end

  def volume_drive
    volume_info.split[2]
  end

  def script
    "select volume=#{volume_number} \nassign letter=#{volume_drive}"
  end

  def save(output)
    File.open(output, 'w') {|f| f.puts script }
  end

  def safe_delete(file_name)
    File.delete(file_name) if File.exists?(file_name)
  end

  def clean_temp_files
    safe_delete(@details_script_file)
    safe_delete(@details_output_file)
    safe_delete(@unfreeze_script_file)
  end

end